package com.sparrows.pirates.helpers;

import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class Animator {
	
	private Animation anim;
	private float stateTime;
	private TextureRegion currentFrame;
	private boolean shouldLoop;
	private int numFrames;
	
	public Animator(float frameRate, TextureRegion[] texReg, int numFrames, boolean loop){
		//Reduce by 1 since animations begin from the '0' frame
		this.numFrames = numFrames - 1;
		//Create animation
		anim = new Animation(frameRate, texReg);
		//Set timer to count frames
		stateTime = 0f;
		//Should the animation loop?
		shouldLoop = loop;
	}
	
	//Step forward animation
	//Should be called early in the render method
	public void update(float dt){
		stateTime += dt;
		currentFrame = anim.getKeyFrame(stateTime, shouldLoop);
	}
	
	//Draw the animation using the passed SpriteBatch and overloading for the other given parameters
	public void draw(SpriteBatch batch, float x, float y){
		batch.draw(currentFrame, x, y);
	}
	public void draw(SpriteBatch batch, float x, float y, float scaleX, float scaleY, float rotation){
		batch.draw(currentFrame, x, y, 0, 0, currentFrame.getRegionWidth(), currentFrame.getRegionHeight(), scaleX, scaleY, rotation);
	}

	public float getCurrentFrame(){
		return anim.getKeyFrameIndex(this.stateTime);
	}
	public int getNumFrames(){
		return this.numFrames;
	}
}
